package tests;

import common.Utils;
import org.openqa.selenium.By;

import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author 李夏
 * @version 1.0
 */

/**
 * 注册页面测试
 */
public class SignUpPage extends Utils {
    public static String url = "http://127.0.0.1:58080/sign-up.html";
    public SignUpPage() {
        super(url);
    }

    /*
    检查页面是否加载成功
     */
    public void  signUpPageRight() throws InterruptedException, IOException {
        //通过查看页面元素是否存在来检查页面加载成功与否
        driver.findElement(By.cssSelector("#signUpForm > div > h2"));
        //登陆输入框
        driver.findElement(By.cssSelector("#username"));

    }
    /*
    用户名、昵称、密码以及确认密码均为未填，未填写完整，注册失败
     */
    public void signUpFail01() throws IOException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#submit")).click();
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > div"));
        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
    }
    /*
    输入用户名在数据库中已存在，注册失败
     */
    public void signUpFail02() throws IOException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("小新");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("小新");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("123456");
        driver.findElement(By.cssSelector("#policy")).click();
        driver.findElement(By.cssSelector("#submit")).click();

        String res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();

        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);
        assert res.equals("用户已存在");

    }
    /*
    输入用户名在数据库中不存在，密码和确认密码不一致，注册失败
     */
    public void signUpFail03() throws IOException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("阿姨");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("阿姨");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("654322222222221");
        driver.findElement(By.cssSelector("#policy")).click();
        driver.findElement(By.cssSelector("#submit")).click();

        //请检查确认密码
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div"));

        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);

    }
    /*
    输入用户名在数据库中不存在，密码和确认密码一致，未勾选同意使用条款和隐私政策，注册失败

     */
    public void signUpFail04() throws IOException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("阿姨");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("阿姨");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("123456");
//        driver.findElement(By.cssSelector("#policy")).click();
        driver.findElement(By.cssSelector("#submit")).click();

        //还在注册页面，说明跳转失败
        driver.findElement(By.cssSelector("#signUpForm > div > h2"));

        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);

    }
    /*
    输入的用户名数据库中不存在，昵称不为空，密码与确认密码一致并且勾选同意使用条款和隐私政策，注册成功后跳转至登录界面
     */
    public void signUpFail05() throws IOException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("阿姨12");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("阿姨12");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("123456");
        driver.findElement(By.cssSelector("#policy")).click();
        driver.findElement(By.cssSelector("#submit")).click();

        //注册成功，跳转至登录页面，说明测试成功
        String text = driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")).getText();
        assert text.equals("用户登录");

        Method method = new Object() {
        }.getClass().getEnclosingMethod();
        String methodName = method.getName();
        getScreenShot(getClass().getName(),methodName);

    }


}
